package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.MachineDao;
import com.wsd.smartcarwasher.dao.PriceMapper;
import com.wsd.smartcarwasher.domain.Machine;
import com.wsd.smartcarwasher.domain.Price;
import com.wsd.smartcarwasher.domain.PriceExample;
import com.wsd.smartcarwasher.util.MathUtils;
import com.wsd.smartcarwasher.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PriceService {

    @Resource
    private PriceMapper priceMapper;//riceMapper
    @Resource
    private PriceTimeService priceTimeService;
    @Resource
    private MachineService machineService;

    public List<Price> getPrice() {
        PriceExample ex = new PriceExample();
        PriceExample.Criteria ca =  ex.createCriteria();
        ca.andEtimeGreaterThan(new Date());
        //priceMapper.selectByExample(ex);
        return priceMapper.selectByExample(ex);
    }
    public List<Price> getMachinePrice(int machineId) {

        Map<String,Object> node= this.priceTimeService.isUsePlatPrice(machineId);
        if(node==null)
        {
            return null;
        }
        Map<Integer,Object> map=this.priceTimeService.matchingPrice(node);

        //机器对象
        Machine machine=this.machineService.getMachineByMachineId(machineId);
        List<String> machinePriceList=new ArrayList<>();
        if(!StringUtil.isNullOrEmpty(machine))
        {
            if(!StringUtil.isNullOrEmpty(machine.getPriceIds()))
            {
                String s=machine.getPriceIds();
                String [] s1=s.split(",");
                machinePriceList= Arrays.asList(s1);
            }
        }



        if(map.isEmpty())
        {
            PriceExample ex = new PriceExample();
            PriceExample.Criteria ca =  ex.createCriteria();
            ca.andEtimeGreaterThan(new Date());
            //priceMapper.selectByExample(ex);
            return priceMapper.selectByExample(ex);
        }
        PriceExample ex = new PriceExample();
        PriceExample.Criteria ca =  ex.createCriteria();
        ca.andEtimeGreaterThan(new Date());
        List<Price> getPriceList=priceMapper.selectByExample(ex);

        List<Price> PriceRoleList=new ArrayList<>();
        for (Price price:getPriceList
             ) {

                Price newprice=new Price();
                newprice.setTotalprice(MathUtils.getBigDecimal(map.get(price.getType())));
                newprice.setNowPrice(MathUtils.getBigDecimal(map.get(price.getType())));
                newprice.setType(price.getType());
                newprice.setDescribe(price.getDescribe());
                newprice.setId(price.getId());
                newprice.setName(price.getName());
                PriceRoleList.add(newprice);
        }

        List<Price> priceList=new ArrayList<>();
        if(!StringUtil.isNullOrEmpty(machinePriceList))
        {
            for (Price price:PriceRoleList
                 ) {
                if(machinePriceList.contains(price.getId().toString()))
                {
                    priceList.add(price);
                }
            }

            return priceList;
        }

        return PriceRoleList;
    }

    public int priceAdd(Price price) {
        return priceMapper.insertSelective(price);
    }
    public Price getPriceByServiceId(Integer serviceId)
    {
        return priceMapper.selectByPrimaryKey(serviceId);
    }
    public int priceUpdate(Price price) {
        return priceMapper.updateByPrimaryKeySelective(price);
    }

    public List<Price> getAlPrice() {
        PriceExample ex = new PriceExample();
        //priceMapper.selectByExample(ex);
        return priceMapper.selectByExample(ex);
    }

    public int deletePrice(Integer id) {
        return priceMapper.deleteByPrimaryKey(id);
    }
}
